<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>分类参数</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区 -->
    <el-card style="margin-top:20px;">
      <el-row :gutter="50">
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="queryinfo.query" clearable @clear="getGoodsList">
            <el-button slot="append" icon="el-icon-search" @click="search"></el-button>
          </el-input>
        </el-col>
        <el-col :span="5">
          <el-button type="success" @click="goodsAdd">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 表格区域 -->
      <el-table :data="goodsList" border stripe style="width: 100%">
        <el-table-column prop="goods_name" label="商品名称" width="500"></el-table-column>
        <el-table-column prop="goods_number" label="数量"></el-table-column>
        <el-table-column prop="goods_price" label="价格/💴"></el-table-column>
        <el-table-column prop="goods_weight" label="重量"></el-table-column>
        <el-table-column label="创建时间" width="180">
          <template slot-scope="scope">{{scope.row.add_time | time}}</template>
        </el-table-column>
        <el-table-column label="操作" width="180">
          <template slot-scope="scope">
            <el-button
              type="warning"
              size="mini"
              class="el-icon-edit"
              @click="goodsEdite(scope.row)"
            >修改</el-button>
            <el-button
              type="danger"
              size="mini"
              class="el-icon-delete"
              @click="goodsDel(scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryinfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryinfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
  </div>
</template>
<script>
export default {
  data() {
    return {
      // 商品列表数据
      goodsList: [],
      //   请求数据
      queryinfo: {
        query: "",
        pagenum: 1,
        pagesize: 5
      },
      // 总页数
      total: 0
    };
  },
  methods: {
    async getGoodsList() {
      const { data: res } = await this.$http.get("goods", {
        params: this.queryinfo
      });
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      this.goodsList = res.data.goods;
      this.total = res.data.total;
    },
    // 添加商品
    goodsAdd() {
      this.$router.push("/goods/add");
    },
    // 搜索功能
    search() {
      this.queryinfo.pagenum = 1;
      this.getGoodsList();
    },
    // 修改商品
    goodsEdite() {},
    // 删除商品
    async goodsDel(row) {
      let del = await this.$confirm(
        `此操作将永久删除${row.goods_name}, 是否继续?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).catch(err => err);
      if (del !== "confirm") return this.$message.info("取消了删除");
      const { data: res } = await this.$http.delete("goods/" + row.goods_id);
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      this.$message.success(res.meta.msg);
      this.getGoodsList();
    },
    handleSizeChange(newsize) {
      this.queryinfo.pagesize = newsize;
      this.getGoodsList();
    },
    handleCurrentChange(newnum) {
      this.queryinfo.pagenum = newnum;
      this.getGoodsList();
    }
  },
  created() {
    this.getGoodsList();
  }
};
</script>
<style lang="less" scoped>
</style>